package thread;

import java.util.Random;
import java.util.concurrent.LinkedBlockingQueue;

public class PCModel {
    private static LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

    public static void main(String[] args) {
        // Producer
        Thread producer = new Thread(() -> {
            Random rand = new Random();
            for (int i = 0; i < 5; i++) {
                try {
                    int data = rand.nextInt();
                    queue.put(data);
                    System.out.println("Produce data: " + data);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        // Consumer
        Thread consumer = new Thread(() -> {
            for (int i = 0; i < 5; i++) {
                try {
                    Integer data = queue.take();
                    System.out.println("Consume data: " + data);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
        consumer.start();
    }
}
